package com.videoaggre.zcl.ui.activity.agefans

import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.videoaggre.zcl.R
import com.videoaggre.zcl.bean.NewAnime
import com.videoaggre.zcl.common.tabData
import com.videoaggre.zcl.databinding.AgefansItemTimeTableBinding
import com.videoaggre.zcl.helper.OnTabSelectedListener
import com.videoaggre.zcl.helper.newInstance
import com.videoaggre.zcl.parse.impl.AgefansParse
import com.videoaggre.zcl.ui.activity.BaseActivity
import com.videoaggre.zcl.ui.adapter.BaseRecyclerAdapter
import com.videoaggre.zcl.ui.adapter.BaseViewHolder
import kotlinx.android.synthetic.main.activity_time_table.*
import java.util.*
import kotlin.collections.ArrayList

class AgefansTimeTableActivity : BaseActivity(R.layout.activity_time_table) {
    companion object {
        const val TIME_TABLE = "mTimeTable"
    }

    private val mTimeTable: ArrayList<NewAnime> by lazy { intent?.extras?.getParcelableArrayList(TIME_TABLE)!! }
    private val mAgefansTimeTableAdapter: AgefansTimeTableAdapter by newInstance()
    override fun initView() {
        tlTime.tabData {
            tab("周一")
            tab("周二")
            tab("周三")
            tab("周四")
            tab("周五")
            tab("周六")
            tab("周日")
        }
        val week = getWeek()
        tlTime.post { tlTime.setScrollPosition(week,0f,true) }
        tlTime.addOnTabSelectedListener(OnTabSelectedListener { change(tlTime.selectedTabPosition) })
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = mAgefansTimeTableAdapter
        change(week)
    }

    fun getWeek() = when (Calendar.getInstance().get(Calendar.DAY_OF_WEEK)) {
        1 -> 6
        2 -> 0
        3 -> 1
        4 -> 2
        5 -> 3
        6 -> 4
        7 -> 5
        else -> 0
    }

    private fun change(position: Int) {
        if (position < mTimeTable.size) {
            mAgefansTimeTableAdapter.datas = mutableListOf<NewAnime>().apply {
                mTimeTable.forEach {
                    if ((position == 6 && it.wd == 0) || (position == it.wd - 1)) {
                        add(it)
                    }
                }
            }
            mAgefansTimeTableAdapter.notifyDataSetChanged()
        }
    }
}

class AgefansTimeTableAdapter : BaseRecyclerAdapter<BaseViewHolder<AgefansItemTimeTableBinding>, NewAnime>() {
    override fun onCreateViewHolder(parent: ViewGroup, p1: Int) = BaseViewHolder(AgefansItemTimeTableBinding.inflate(getLayoutInflate(parent.context), parent, false))
    override fun onBindViewHolder(holder: BaseViewHolder<AgefansItemTimeTableBinding>, bean: NewAnime, position: Int) {
        holder.mViewBinding.run {
            bean.run {
                tvName.text = name
                tvRankValue.text = namefornew
                setViewClick(position, root)
            }
        }
    }

    override fun subOnClickAdapter(v: View, position: Int) {
        AgefansDetailsActivity.open(v.context, AgefansParse.createDetailLink(getItem(position).id))
    }
}